<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller\reimbursement;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\reimbursement\ExpenseApplicationLists;
use app\adminapi\lists\user\UserLists;
use app\adminapi\logic\reimbursement\ExpenseApplicationLogic;
use app\adminapi\validate\reimbursement\ExpenseApplicationValidate;
use app\common\model\user\User;
use think\facade\Db;


/**
 * ExpenseApplication控制器
 * Class ExpenseApplicationController
 * @package app\adminapi\controller\reimbursement
 */
class ExpenseApplicationController extends BaseAdminController
{


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function lists()
    {
        return $this->dataLists(new ExpenseApplicationLists());
    }

    /**
     * @notes 添加报账记录
     * @return \think\response\Json
     */
    public function add()
    {
        $params = $this->request->post();
        $validate = new ExpenseApplicationValidate();
        if(!$validate->scene('add')->check($params)){
            return $this->fail($validate->getError());
        }
            $result = ExpenseApplicationLogic::add($params);
        if ($result['success']) {
            return $this->success('申请提交成功', $result['data']);
        }
        return $this->fail($result['message']);
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function edit()
    {
        $params = $this->request->post();
        $validate = new ExpenseApplicationValidate();
        if (!$validate->scene('edit')->check($params)) {
            return $this->fail($validate->getError());
        }
        $result = ExpenseApplicationLogic::edit($params);
        if ($result['success']) {
            return $this->success($result['message'], $result['data']);
        }
        return $this->fail($result['message']);
    }


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function delete()
    {
        $params = $this->request->post();
        $validate = new ExpenseApplicationValidate();
        if (!$validate->scene('delete')->check($params)) {
            return $this->fail($validate->getError());
        }
        $result = ExpenseApplicationLogic::delete($params);
        if ($result['success']) {
            return $this->success($result['message']);
        }
        return $this->fail($result['message']);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/05/16 11:36
     */
    public function detail()
    {
        $params = (new ExpenseApplicationValidate())->goCheck('detail');
        $result = ExpenseApplicationLogic::detail($params);
        if ($result['success']) {
            return $this->success('获取成功', $result['data']);
        }
        return $this->fail($result['message']);
    }

    /**
     * @notes 审批报账申请
     * @return \think\response\Json
     */
    public function approve()
    {
        $params = $this->request->post();
        $validate = new ExpenseApplicationValidate();
        if (!$validate->scene('approve')->check($params)) {
            return $this->fail($validate->getError());
        }
        
        $result = ExpenseApplicationLogic::approve($params);
        if ($result['success']) {
            return $this->success($result['message'], $result['data']);
        }
        return $this->fail($result['message']);
    }

    /**
     * @notes 获取操作日志
     * @return \think\response\Json
     */
    public function operationLogs()
    {
        $expenseId = $this->request->get('expense_id/d');
        if (empty($expenseId)) {
            return $this->fail('请提供报账申请ID');
        }

        $result = ExpenseApplicationLogic::getOperationLogs($expenseId);
        if ($result['success']) {
            return $this->success('获取成功', $result['data']);
        }
        return $this->fail($result['message']);
    }

    /**
     * 获取申请人下拉列表
     */
    public function getApplicants()
    {
        $result = ExpenseApplicationLogic::getApplicantsList();
        if ($result['success']) {
            return $this->success('获取成功', $result['data']);
        }
        return $this->fail($result['message']);
    }

    /**
     * 获取审批状态下拉选项
     * @return \think\response\Json
     */
    public function getApproveStatusOptions()
    {
        $options = [
            ['label' => '通过', 'value' => 3],
            ['label' => '退回', 'value' => 2]
        ];
        return $this->success('获取成功', $options);
    }

}